--
-- Author: mm
-- Date: 2018-03-19 14:55:37
--
-- NewguildView
-- 工会主页

local TabsBtn = require("app.widget.TabsBtn")
local NewguildHomePagePanel = import(".NewguildHomePagePanel")
local NewguildStudyPanel = import(".NewguildStudyPanel")
-- local  = import(".")
local NewguildChallengePanel = import(".NewguildChallengePanel")
local UniversalShopPanel = require("app.prestige.views.UniversalShopPanel")
local NewguildManagePanel = import(".NewguildManagePanel")
local PeeragesPanel = import(".PeeragesPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewGuildUI

local tabBtnNum = 6 -- 功能按钮个数
local rightBtnPanelInitPosY = {}

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	self._tltPanel = self._ui.Panel_tlt
	-- tabs
	self._ui.home:setVisible(false) --默认隐藏主页panel(要不然直接选中商店页签，UI上默认的panel会穿帮)
	self._binds = {}
	local btns = {0,0,0,0,0,0}
	for i = 1, tabBtnNum do
		btns[i] = self._ui.Panel_tab:getChildByName("selected_btn_" .. i)
		table.insert( rightBtnPanelInitPosY, btns[i]:getPositionY() )
	end
	local function onTabsBtn(index, btn)
		if self._curIdx then
			self._curIdx = index
			self:switchPanel()
		else
			self._curIdx = index
		end
	end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1, self._binds)

	self:_adjustPos()
end

function ClassRef:_adjustPos()
	-- 以下：动态调整posY
	local visibleBtnNum = 0 -- 可见按钮个数
	for i = 1, tabBtnNum do
		if self._ui.Panel_tab:getChildByName("selected_btn_" .. i):isVisible() then
			visibleBtnNum = visibleBtnNum + 1
			self._ui.Panel_tab:getChildByName("selected_btn_" .. i):setPositionY( rightBtnPanelInitPosY[visibleBtnNum] )
		end
	end
end

function ClassRef:show(index)
	self:setVisible(true)
	self:switchPanel(index)
	self:openAction()
end

-- 管理panel是否可见
function ClassRef:refreshManagePanelVisible( visible )
	self._tabsBtn:getBtn( 6 ):setVisible( visible )
end

function ClassRef:updateGoldInfo()
	if self._curIdx == 5 then
		local shopPanel = self:getPanelById(5)
		if shopPanel then
			shopPanel:updateGoldInfo()
		end
	end
end

function ClassRef:switchPanel(index)
	if index then
		self._curIdx = index
		self._tabsBtn:setSelected(index)
	end

	self:createPanel(self._curIdx)
	executeFunc(self._onSwitchPanel, self._curIdx)

	if self._curIdx == 5 then
		self._tltPanel:setVisible(false)
		executeFunc(self._onOperate, "showGuildShop")
	else
		self._tltPanel:setVisible(true)
	end
	for k, panel in pairs(self._binds) do
        if panel then
            if tonumber(k) == self._curIdx then
                panel:setVisible(true)
            else
                panel:setVisible(false)
            end
        end
    end
end

function ClassRef:createPanel(index)
	if not self._binds[index] then
		local panel
		if index == 1 then
			panel = NewguildHomePagePanel.create( self._ui.home )
		elseif index == 2 then
			panel = NewguildStudyPanel.create( self._ui.study )
		elseif index == 3 then
			panel = PeeragesPanel.create(self._ui.emperor)
		elseif index == 4 then
			panel = NewguildChallengePanel.create( self._ui.challenge )
		elseif index == 5 then
			panel = UniversalShopPanel.create( self._ui.shop )
		elseif index == 6 then
			panel = NewguildManagePanel.create( self._ui.manage )
		end
		if panel.onOperate then
			panel:onOperate(self._onOperate)
		end
		self._binds[index] = panel
	end
end

function ClassRef:getPanelById( idx )
	return self._binds[ idx ]
end

-- 管理panel上新申请标记
function ClassRef:refreshNewApplyTips( applyTips )
	self._ui.Panel_tab:findChild("selected_btn_6/dot"):setVisible( applyTips )
end

function ClassRef:refreshChallengeTips( haveBoxGet )
	self._ui.Panel_tab:findChild("selected_btn_4/dot"):setVisible( haveBoxGet )
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end


return ClassRef
